package com.zsqat.eshop.ware.service.impl;

import com.zsqat.common.utils.R;
import com.zsqat.eshop.ware.feign.ProductFeignService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zsqat.common.utils.PageUtils;
import com.zsqat.common.utils.Query;
import com.zsqat.eshop.ware.dao.WareSkuDao;
import com.zsqat.eshop.ware.entity.WareSkuEntity;
import com.zsqat.eshop.ware.service.WareSkuService;
import org.springframework.transaction.annotation.Transactional;


@Service("wareSkuService" )
public class WareSkuServiceImpl extends ServiceImpl<WareSkuDao, WareSkuEntity> implements WareSkuService {
  @Autowired
  private WareSkuDao wareSkuDao;

  @Autowired
  private ProductFeignService productFeignService;

  @Override
  public PageUtils queryPage(Map<String, Object> params) {
    QueryWrapper<WareSkuEntity> wrapper = new QueryWrapper<>();

    String skuId = (String) params.get("skuId");
    if ((!StringUtils.isEmpty(skuId)) && (!"0".equalsIgnoreCase(skuId))) {
      wrapper.eq("sku_id", skuId);
    }

    String wareId = (String) params.get("wareId");
    if ((!StringUtils.isEmpty(wareId)) && (!"0".equalsIgnoreCase(wareId))) {
      wrapper.eq("ware_id", wareId);
    }

    IPage<WareSkuEntity> page = this.page(
        new Query<WareSkuEntity>().getPage(params),
        wrapper
    );

    return new PageUtils(page);
  }

  @Transactional(rollbackFor = Exception.class)
  @Override
  public void addStock(Long skuId, Long wareId, Integer skuNum) {
    //1、判读如果没有这个库存记录新增
    List<WareSkuEntity> wareSkuEntities = wareSkuDao.selectList(
        new QueryWrapper<WareSkuEntity>().eq("sku_id", skuId).eq("ware_id", wareId));

    if (wareSkuEntities == null || wareSkuEntities.size() == 0) {
      WareSkuEntity wareSkuEntity = new WareSkuEntity();
      wareSkuEntity.setSkuId(skuId);
      wareSkuEntity.setStock(skuNum);
      wareSkuEntity.setWareId(wareId);
      wareSkuEntity.setStockLocked(0);
      // TODO 远程查询sku的名字，如果失败整个事务无需回滚，
      // 不能因skuname没找到，导致整个事务回滚
      //1、自己catch异常
      try{
        R info = productFeignService.info(skuId);
        Map<String,Object> data = (Map<String, Object>) info.get("skuInfo");
        if (info.getCode() == 0) {
          wareSkuEntity.setSkuName((String) data.get("skuName"));
        }
      } catch (Exception e) {

      }
      //添加库存信息
      wareSkuDao.insert(wareSkuEntity);
    } else {
      //修改库存信息
      wareSkuDao.addStock(skuId,wareId,skuNum);
    }
  }

}